﻿using System;
using System.Windows;
using System.Windows.Input;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;
using Autorent.Desktop.Main;
using Autorent.Desktop.Model;
using Autorent.Desktop.Model.AutorentMngrServiceReference;
using Autorent.Desktop.Model.Services;
using Autorent.Desktop.Modules.Addresses.Views;
using Autorent.Desktop.Modules.Addresses.ViewModels;
using Autorent.Desktop.Modules.Events;
using Autorent.Desktop.Modules.Offices.Services;
using Autorent.Desktop.Modules.Offices.Views;

namespace Autorent.Desktop.Modules.Offices.ViewModels
{
    public class CreateOfficeViewModel
    {
        #region Public Properties

        public CreateOfficeView View { get; set; }

        public Office Office { get; set; }

        #endregion

        #region Constructors

        public CreateOfficeViewModel(CreateOfficeView view, IOfficesRepository repository, IUnityContainer container, 
            IEventAggregator eventAggregator, IRegionManager regionManager)
        {
            this.View = view;
            view.Model = this;

            _container = container;
            _repository = repository;
            _eventAggregator = eventAggregator;
            _eventAggregator.GetEvent<AddressCreatedEvent>().Subscribe(CreateAddressEventHandler);
            _eventAggregator.GetEvent<ViewClosedEvent>().Subscribe(ViewClosedEventHandler);
            _regionManager = regionManager;

            this.Office = new Office();
        }

        #endregion

        #region Commands

        public ICommand SaveOffice
        {
            get
            {
                return new RelayCommand(param => saveOffice());
            }
        }

        public ICommand Cancel
        {
            get
            {
                return new RelayCommand(param => cancel());
            }
        }

        public ICommand CreateAddress
        {
            get
            {
                return new RelayCommand(param => createAddress());
            }
        }

        #endregion

        #region Private Variables

        private readonly IOfficesRepository _repository;
        private readonly IEventAggregator _eventAggregator;
        private readonly IRegionManager _regionManager;
        private readonly IUnityContainer _container;

        #endregion

        #region Private Methods

        private void saveOffice()
        {
            _repository.AddOffice(this.Office);
            
            _eventAggregator.GetEvent<CollectionChangedEvent>().Publish("Offices");
            
            _regionManager.Regions["DetailsRegion"].Remove(View);
        }

        private void cancel()
        {
            _regionManager.Regions["DetailsRegion"].Remove(View);
        }

        private void createAddress()
        {
            CreateOfficeView createOfficeView = (CreateOfficeView)_regionManager.Regions["DetailsRegion"].GetView("CreateOfficeView");

            if (createOfficeView != null)
            {
                createOfficeView.Visibility = System.Windows.Visibility.Collapsed;

                CreateAddressView view = _container.Resolve<CreateAddressViewModel>().View;

                _regionManager.Regions["DetailsRegion"].Add(view);
                _regionManager.Regions["DetailsRegion"].Activate(view);
            }
        }

        private void CreateAddressEventHandler(Address address)
        {
            this.Office.Street_ID = address.Street.Street_ID;
            this.Office.Street = address.Street;
            this.Office.Street.City = address.City;
            this.Office.Street.City.Region = address.Region;
            this.Office.Street.City.Region.Country = address.Country;

            CreateOfficeView createOfficeView = (CreateOfficeView)_regionManager.Regions["DetailsRegion"].GetView("CreateOfficeView");
            if (createOfficeView != null)
            {
                createOfficeView.Visibility = Visibility.Visible;
                _regionManager.Regions["DetailsRegion"].Activate(createOfficeView);
            }
        }

        private void ViewClosedEventHandler(Type viewType)
        {
            if (viewType == typeof(CreateAddressView))
            {
                CreateOfficeView createOfficeView = (CreateOfficeView)_regionManager.Regions["DetailsRegion"].GetView("CreateOfficeView");
                if (createOfficeView != null)
                {
                    createOfficeView.Visibility = Visibility.Visible;
                    _regionManager.Regions["DetailsRegion"].Activate(createOfficeView);
                }
            }
        }

        #endregion
    }
}
